package com.fndsoft.logstorage.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.fndsoft.logstorage.component.colog.COLog;

public class HttpDownload {
	
	public int downFile(String path,String fileName, InputStream inputStream) {
		try{
			FileUtils fileUtils = new FileUtils();
			//如果已经存在这个文件
			if(fileUtils.isFileExist(path+fileName)) {
				return 1;
			} else {
				File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream);
				inputStream.close();
				if(resultFile==null) {
					return -1;
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		} 
		return 0;
	}
	
	public int downFile2(String path,String fileName, InputStream inputStream) {
		try{
			FileUtils fileUtils = new FileUtils();
			//如果已经存在这个文件
			if(fileUtils.isFileExist(path+fileName)) {
				BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileUtils.createFile(path + fileName))));
				String line = br.readLine();
				String date = line.substring(0, 10);
				String getDate = fileUtils.getDateString();
				System.out.println("getDate~~~~~" + getDate);
				if(!date.equals(getDate)) {
					fileUtils.deleteFile(path+fileName);
					fileUtils.write2SDFromInput2(path, fileName, inputStream);
				}
				if(date.equals(getDate)) {
					fileUtils.write2SDFromInput2(path+fileName, inputStream);
				}
				return 1;
			} else {
				File resultFile = fileUtils.write2SDFromInput2(path, fileName, inputStream);
				inputStream.close();
				if(resultFile==null) {
					return -1;
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		} 
		return 0;
	}
	
}
